//
//  statusCell.swift
//  MySinaWeiBo
//
//  Created by zhangfuwei on 15/8/3.
//  Copyright © 2015年 zhangfuwei. All rights reserved.
//

import UIKit
/// 控件间距
let statusCellControlMargin: CGFloat = 8.0

enum statusCellReuseIdentifier:String {
   
    case StatusNormalCell = "StatusNormalCell"
    case StatusForwardCell = "StatusForwardCell"
    
   static func reuseID(VCstatus:Status) -> String {
      return  VCstatus.retweeted_status == nil ? statusCellReuseIdentifier.StatusNormalCell.rawValue : statusCellReuseIdentifier.StatusForwardCell.rawValue
    }
}

class statusCell: UITableViewCell {

    ///  定义一个微博模型属性
    
    var status: Status? {
        didSet {
            
            topV.status = status
            ///  给文本设置 text
            contentLable.text = status?.text ?? ""

            pictureV.status = status
            ///  给配图视图设置约束
            pictureWidthCons?.constant = pictureV.bounds.size.width
            
            pictureHeightCons?.constant = pictureV.bounds.size.height
            
            pictureTopCons?.constant = (pictureV.bounds.height == 0) ? 0: statusCellControlMargin
            
        }
    }
    
    func rowHeight(VCstatus: Status) ->CGFloat {
        self.status = VCstatus
        
        layoutIfNeeded()
        
        return CGRectGetMaxY(buttomV.frame)
        
    }
    
    /// 图片宽度约束
    var pictureWidthCons: NSLayoutConstraint?
    /// 图片高度约束
    var pictureHeightCons: NSLayoutConstraint?
    
    /// 图片顶部约束
    var pictureTopCons: NSLayoutConstraint?
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
    }

    
    // MARK:  设置 UI 界面
    func setupUI() {
        
        ///  添加控件
        contentView.addSubview(addview)
        contentView.addSubview(topV)
        contentView.addSubview(contentLable)
        contentView.addSubview(pictureV)
        contentView.addSubview(buttomV)
        
        /// 设置约束
        
        // 0> 顶部视图
        addview.ff_AlignInner(type: ff_AlignType.TopLeft, referView: contentView, size:CGSize(width: UIScreen.mainScreen().bounds.size.width, height: 10))
        // 1> 顶部视图
        topV.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: addview, size: CGSize(width:UIScreen.mainScreen().bounds.size.width, height: 53))
        
        
        // 2> 文本视图
        contentLable.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: topV, size: nil,offset: CGPoint(x: statusCellControlMargin, y: statusCellControlMargin))
        
        contentView.addConstraint(NSLayoutConstraint(item: contentLable, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Width, multiplier: 1.0, constant: -2 * statusCellControlMargin))
        
       
//        // 3> 配图视图
//      
//        let cons = pictureV.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: contentLable, size: CGSize(width: 290, height: 90),offset: CGPoint(x: 0, y: statusCellControlMargin))
//        
//        pictureHeightCons = pictureV.ff_Constraint(cons, attribute: NSLayoutAttribute.Height)
//        pictureWidthCons = pictureV.ff_Constraint(cons, attribute: NSLayoutAttribute.Width)
//        pictureTopCons = pictureV.ff_Constraint(cons, attribute: NSLayoutAttribute.Top)
        
        // 4> 底部视图
        buttomV.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: pictureV, size: CGSize(width: UIScreen.mainScreen().bounds.size.width, height: 50),offset: CGPoint(x: -statusCellControlMargin, y: statusCellControlMargin))
    
    }
    
    
    ///  懒加载分割视图
    lazy var addview : UIView = {
       
        let v = UIView()

        v.backgroundColor = UIColor(white: 0.9, alpha: 1.0)
        return v
        
        }()
    
    ///  懒加载顶部视图
    lazy var topV: topView  = topView()
    
   
    ///  懒加载文本视图
    lazy var contentLable:UILabel = {

        let contentLbl = UILabel(color: UIColor.darkGrayColor(), fontSize: 16)
        contentLbl.numberOfLines = 0

        return contentLbl

        }()
    ///  添加配图视图
    lazy var pictureV:StatusPictureView = StatusPictureView()
    ///  懒加载底部视图
    lazy var buttomV : buttomView = buttomView()
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}
